package com.example.shopsystem.control;

import com.example.shopsystem.aop.GetIdUtil;
import com.example.shopsystem.entity.BusCost;
import com.example.shopsystem.entity.Business;
import com.example.shopsystem.entity.Order;
import com.example.shopsystem.entity.User;
import com.example.shopsystem.service.BusinessServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/filter/business")
public class BusinessControl {
    @Autowired
    BusinessServiceImpl businessService;


    @PostMapping("/ViewInvitedCustomers")
    public Result<Object> ViewInvitedCustomers(@RequestBody Business business) {//查看所有下级
        business.setId(GetIdUtil.getCurrentUser());
        if (business.getId() == null) {
            return Result.phoneNull(null);
        }
        List<User> userList = businessService.ViewInvitedCustomers(business);
        if (userList == null) {
            return Result.queryFail(null);
        }
        return Result.ok(userList);
    }

    @PostMapping("/ViewProductList")
    public Result<Object> ViewProductList(@RequestBody Business business) {//查看商品
        business.setId(GetIdUtil.getCurrentUser());
        return Result.ok(businessService.ViewProductList());
    }

    @PostMapping("/invitation")
    public Result<Object> invitation(@RequestBody User user, Business business) {//邀请注册
        business.setId(GetIdUtil.getCurrentUser());
        if (user.getPhone() == null || user.getPwd() == null) {//为空判断
            return Result.phoneNull(null);
        }
        if (user.getPhone().length() != 11 || user.getPwd().length() < 6) {
            return Result.phoneBad(null);//字符规则判断
        }
        if (!businessService.invitation(user, business.getId()))
            return Result.intFail(null);
        return Result.ok(null);
    }

    @PostMapping("/receiverRemind")
    public Result<Object> receiverRemind(@RequestBody Order order) {
        System.out.println(order.getId());
        if (!businessService.receiverRemind(order.getId())) {
            return Result.Fail(null);
        }

        return Result.ok(null);
    }

    @PostMapping("/busViewInfo")
    public Result<Object> busViewInfo(@RequestBody Business business) {
        business.setId(GetIdUtil.getCurrentUser());
        if (business.getId() == null) return Result.queryFail(null);
        return Result.ok(businessService.busViewInfo(business.getId()));
    }

    @PostMapping("/busModifyInfo")
    public Result<Object> busModifyInfo(@RequestBody Business business) {
        business.setId(GetIdUtil.getCurrentUser());
        if (!businessService.busModifyInfo(business)) {
            return Result.Fail(null);
        }
        return Result.ok(null);
    }

    @PostMapping("/busWithdrawal")
    public Result<Object> busWithdrawal(@RequestBody Map map) {
        map.put("id", GetIdUtil.getCurrentUser());
        Business business1 = businessService.busViewInfo(String.valueOf(map.get("id")));

        if (String.valueOf(business1.getMoneyPwd()) == null) {//验证密码是否为空
            return Result.phoneNull(null);
        }
        if (!String.valueOf(business1.getMoneyPwd()).equals(String.valueOf(map.get("moneyPwd")))) {//验证密码
            return Result.phoneBad(null);
        }

        if (!businessService.busWithdrawal(business1, Double.valueOf(map.get("money").toString()))) {
            return Result.mdFail(null);
        }


        return Result.ok(null);
    }

    @PostMapping("/ChangeMoneyPwd")
    public Result<Object> ChangeMoneyPwd(@RequestBody Map map) {//修改支付密码
        map.put("id", GetIdUtil.getCurrentUser());
        if (String.valueOf(map.get("newMoneyPwd")).length() != 6) {
            return Result.formatError(null);
        }
        Business business = businessService.busViewInfo(String.valueOf(map.get("id")));
        System.out.println(business);
        if (!business.getPwd().equals(map.get("pwd"))) {
            return Result.pwdBad(null);
        }
        business.setMoneyPwd(Integer.valueOf(map.get("newMoneyPwd").toString()));
        if (!businessService.updateMoneyPwd(business.getMoneyPwd(), business.getId())) {
            return Result.Fail(null);
        }
        return Result.ok(null);
    }

    @PostMapping("/viewBusCost")
    public Result<Object> viewBusCost(@RequestBody Map map) {//查看账单
        map.put("id", GetIdUtil.getCurrentUser());
        if (map.get("id") == null) {
            return Result.phoneNull(null);
        }
        List<BusCost> costList = businessService.viewCostList(String.valueOf(map.get("id")));
        if (costList.isEmpty()) {
            return Result.queryFail(null);
        }
        return Result.ok(costList);
    }


}
